package cn.rtplay.esp32camcar;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.io.File;
import java.util.Objects;

import cn.rtplay.esp32camcar.databinding.VideoLoadBinding;
import cn.rtplay.esp32camcar.toolUtils.AppVersions;
import cn.rtplay.esp32camcar.toolUtils.LogUtil;

public class VideoLoad extends Fragment implements SurfaceHolder.Callback,
        MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener,
        MediaPlayer.OnErrorListener,
        MediaPlayer.OnInfoListener,
        MediaPlayer.OnVideoSizeChangedListener {
    private static final String TAG = "VideoLoad";
    private VideoLoadBinding mViewBinding;
    private MediaPlayer mPlayer;
    private boolean mIsPrepared;
    private boolean mPrepared;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mViewBinding = VideoLoadBinding.inflate(inflater, container, false);

        //get loading path
        File loading = requireActivity().getExternalFilesDir("load");
        String loadingPath = Objects.requireNonNull(loading).getPath() + "/" + AppVersions.getInstance().getLocalLoadName(getActivity()) + ".mp4";
        LogUtil.d(TAG, "onCreateView:loadingPath-> " + loadingPath);
        //init SurfaceView
        mViewBinding.surfaceView.setZOrderOnTop(false);
        mViewBinding.surfaceView.getHolder().addCallback(this);
        //init MediaPlayer
        mPlayer = new MediaPlayer();
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnErrorListener(this);
        mPlayer.setOnInfoListener(this);
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnVideoSizeChangedListener(this);
        mPlayer.setLooping(true);
        try {
            //使用手机本地视频
            //mPlayer.reset();
            mPlayer.setDataSource(loadingPath);
            //mPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(TAG, "initPlayer: error-> " + e);
        }
        return mViewBinding.getRoot();
    }

    /**
     * 播放暂停自动切换
     */
    private void autoPlay() {
        if (mPlayer == null) {
            return;
        }
        if (mPlayer.isPlaying()) {
            mPlayer.pause();
        } else {
            mPlayer.start();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        LogUtil.v(TAG, "onCompletion: in");
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        LogUtil.v(TAG, "onPrepared: in");
        mIsPrepared = true;
        if ((mPlayer != null) && (!mPlayer.isPlaying())) {
            mPlayer.start();
        }
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        LogUtil.v(TAG, "onVideoSizeChanged: in");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        LogUtil.v(TAG, "surfaceCreated: in");
        mPlayer.setDisplay(holder);
        if (!mPrepared) {
            mPrepared = true;
            mPlayer.prepareAsync();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        LogUtil.v(TAG, "surfaceChanged: in");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        LogUtil.v(TAG, "surfaceDestroyed: in");
        if (mPlayer != null) {
            mPlayer.pause();
        }
    }

    @Override
    public void onPause() {
        LogUtil.v(TAG, "onPause: in");
        super.onPause();
    }

    @Override
    public void onStop() {
        LogUtil.v(TAG, "onStop: in");
//        if ((mPlayer != null) && (mPlayer.isPlaying())) {
//            LogUtil.e(TAG, "mPlayer.pause();");
//            mPlayer.pause();
//        }
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.v(TAG, "onResume: in");
        if ((mPlayer != null) && (!mPlayer.isPlaying()) && mIsPrepared) {
            mPlayer.start();
        }
    }
}
